package com.zygk.library.util;

import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.download.DownloadListener;
import com.yanzhenjie.nohttp.download.DownloadQueue;
import com.yanzhenjie.nohttp.download.DownloadRequest;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.RequestQueue;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;

public class CallServer {
  private static CallServer instance;

  public static CallServer getInstance() {
    if (instance == null) {
      synchronized (CallServer.class) {
        if (instance == null) instance = new CallServer();
      }
    }
    return instance;
  }

  private RequestQueue queue;
  private DownloadQueue mDownloadQueue;

  private CallServer() {
    queue = NoHttp.newRequestQueue(5);
    mDownloadQueue = NoHttp.newDownloadQueue(3);
  }

  public <T> void request(int what, Request<T> request, SimpleResponseListener<T> listener) {
    queue.add(what, request, listener);
  }

  public void download(int what, DownloadRequest request, DownloadListener listener) {
    mDownloadQueue.add(what, request, listener);
  }

  // 完全退出app时，调用这个方法释放CPU。
  public void stop() {
    queue.stop();
    mDownloadQueue.stop();
  }
}
